package com.lw.sbdemo2.common.utils;

import com.google.common.collect.ArrayListMultimap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;

/**
 * <p>标题: 获取中文字母工具类</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author:zhongjiabing
 * @date 2018/12/28
 */

public class Py4jDictionary {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private ArrayListMultimap<String, String> duoYinZiMap;
    private static final String CONFIG_NAME = "py4j.dic";
    private static final String PINYIN_SEPARATOR = "#";
    private static final String WORD_SEPARATOR = "/";
    private volatile boolean initialized;

    public Py4jDictionary() {
    }

    public static Py4jDictionary getDefault() {
        return Py4jDictionary.SingletonHolder.INSTANCE;
    }

    public ArrayListMultimap<String, String> getDuoYinZiMap() {
        this.checkInit();
        return this.duoYinZiMap;
    }

    private void checkInit() {
        if (!this.initialized) {
            this.loadVocabulary();
        }

    }

    private synchronized void loadVocabulary() {
        if (!this.initialized) {
            this.duoYinZiMap = this.loadVocabulary0("py4j.dic");
            this.initialized = true;
        }
    }

    private ArrayListMultimap<String, String> loadVocabulary0(String name) {
        this.debug("******start load py4j config******");
        ArrayListMultimap<String, String> duoYinZiMap = ArrayListMultimap.create(512, 32);
        String filename = name;

        try {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Enumeration<URL> urls = cl.getResources(filename);
            if (urls != null) {
                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    this.parseURL(url, duoYinZiMap);
                }
            }
        } catch (Exception var7) {
            this.error("caught exception when load py4j vocabulary", var7);
            throw new RuntimeException("caught exception when load py4j vocabulary", var7);
        }

        this.debug("******load py4j config over******");
        this.debug("py4j map key size:{}", duoYinZiMap.keySet().size());
        return duoYinZiMap;
    }

    private void parseURL(URL url, ArrayListMultimap<String, String> duoYinZiMap) {
        this.debug("load py4j dictionary file:{}", url.getPath());
        InputStream in = null;
        BufferedReader br = null;

        try {
            in = url.openStream();
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = null;

            while((line = br.readLine()) != null) {
                String[] arr = line.split("#");
                if (StringUtils.isNotEmpty(arr[1])) {
                    String[] dyzs = arr[1].split("/");
                    String[] var8 = dyzs;
                    int var9 = dyzs.length;

                    for(int var10 = 0; var10 < var9; ++var10) {
                        String dyz = var8[var10];
                        if (StringUtils.isNotEmpty(dyz)) {
                            duoYinZiMap.put(arr[0], dyz.trim());
                        }
                    }
                }
            }
        } catch (IOException var19) {
            throw new RuntimeException(String.format("load py4j config:%s error", url), var19);
        } finally {
            try {
                br.close();
                in.close();
            } catch (IOException var18) {
                var18.printStackTrace();
            }

        }

    }

    private void error(String msg, Throwable err) {
        this.logger.error(msg, err);
    }

    private void debug(String msg, Object... args) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug(msg, args);
        }

    }

    private static class SingletonHolder {
        private static final Py4jDictionary INSTANCE = new Py4jDictionary();

        private SingletonHolder() {
        }
    }
}
